package model

import (
	"community_end/utils/errmsg"
	"gorm.io/gorm"
)

type Comment struct {
	gorm.Model
	Content string
	PostID  int
	UserID  int
}

func GetCommentsByTopicID(id string) ([]*Comment, int) {
	comments := []*Comment{}
	err := db.Where("post_id = ?", id).Find(&comments).Error
	if err != nil {
		return nil, errmsg.COMMENT_SELECT_FAILED
	}
	return comments, errmsg.SUCCESS
}

func InsertComment(comment Comment) int {
	err := db.Create(&comment).Error
	if err != nil {
		return errmsg.COMMENT_INSERT_FAILED
	}
	return errmsg.SUCCESS
}

func (Comment) TableName() string {
	return "comments"
}
